import Vue from "vue";
import VueRouter from "vue-router";
import CommAll from "../views/community/children/all.vue";
import CommEssence from "../views/community/children/essence.vue";
import CommNew from "../views/community/children/new.vue";
import CommEdit from "../views/community/children/edit.vue";
import Handlock from "../views/admin/adminComponents/Handlock.vue";

Vue.use(VueRouter);

const routes = [
  {
    // 登录主页
    path: "/login",
    component: () => import("../views/login/Login.vue")
  },
  {
    // 注册
    path: "/Register",
    component: () => import("../views/login/Register.vue")
  },
  {
    // 登录
    path: "/online",
    component: () => import("../views/login/online.vue")
  },
  {
    path: "/Handlock",
    component: Handlock
  },
  {
    path: "/",
    component: () => import("../views/Home.vue"),
    redirect: "/Community",
    meta: {
      isLogin: true // 添加该字段，表示进入这个路由是需要登录的
    }, //路由元
    children: [
      {
        path: "/Community",
        component: () => import("../views/community/Community"),
        redirect: "/Community/all",
        children: [
          {
            path: "/Community/all",
            component: CommAll
          },
          {
            path: "/Community/new",
            component: CommNew
          },
          {
            path: "/Community/essence",
            component: CommEssence
          }
        ]
      },
      {
        path: "/Admin",
        component: () => import("../views/admin/admin.vue")
      },

      {
        path: "/Information",
        component: () => import("../views/information/Information.vue")
      },
      {
        path: "/Information/people",
        component: () => import("../views/information/selectPeople.vue")
      }
    ]
  },
  // 编辑页
  {
    path: "/edit",
    component: CommEdit
  },
  {
    path: "/ChatRoom",
    component: () => import("../views/chatRoom/ChatRoom.vue"),
    children: [
      {
        path: "/ChatRoom/SecChatRoom",
        component: () =>
          import("../views/chatRoom/chatRoomComponents/SecChatRoom")
      }
    ]
  },
  //-------------------------------------------------------许
  // 个人详情页
  {
    path: "/DetailsPersonal/:id/:name/:userid",
    name: "DetailsPersonal",
    component: () => import("../components/DetailsPersonal.vue")
  },
  // 个人聊天页
  {
    path: "/onlyinfo/:id/:name/:userid",
    name: "onlyinfo",
    component: () => import("../components/onlyinfo/OnlyInfo")
  },
  //关注页
  {
    path: "/Follows",
    component: () => import("../components/ItFollows.vue")
  },
  // 粉丝页
  {
    path: "/Fans",
    component: () => import("../components/ItFans.vue")
  },
  //帖子详情
  {
    path: "/PostDetail/:uid/:bid",
    name: "PostDetail",
    component: () => import("../components/PostDetail.vue")
  },
  //------------------------------------------
  {
    // 编辑资料
    path: "/EditData",
    component: () => import("../views/admin/adminComponents/EditData.vue")
  },
  //我发布的帖子
  {
    path: "/Mypost",
    component: () => import("../views/admin/adminComponents/MyPost.vue")
  },
  {
    // 谁看过我
    path: "/lookme",
    component: () => import("../views/admin/adminComponents/LookMe.vue")
  },
  // 安全设置
  {
    path: "/Security",
    component: () =>
      import("../views/admin/adminComponents/security/Security.vue")
  },
  // 通用设置
  {
    path: "/CurrencySet",
    component: () => import("../views/admin/adminComponents/CurrencySet.vue")
  },
  // 客服在线
  {
    path: "/CustomerService",
    component: () =>
      import("../views/admin/adminComponents/CustomerService.vue")
  },
  {
    // 互相关注
    path: "/mutualattention",
    component: () =>
      import("../views/admin/adminComponents/MutualAttention.vue"),
    children: [
      {
        path: "/mutualattention",
        component: () => import("../views/admin/adminComponents/Attention.vue")
      },
      {
        path: "/Follow",
        component: () => import("../views/admin/adminComponents/Follow.vue")
      },
      {
        path: "/fan",
        component: () => import("../views/admin/adminComponents/Fans.vue")
      }
    ]
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

// 路由守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆

router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.isLogin)) {
    let token = localStorage.getItem("Authorization");
    // console.log(to);
    //判断是否需要登录

    if (token !== "") {
      // console.log(token);
      next();
    } else {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath
        }
      });
    }
  } else {
    next();
  }
});

// router.beforeEach((to,from,next)=>{
//   if(to.matched.some(res=>res.meta.isLogin)){//判断是否需要登录
//       if (sessionStorage['username']) {
//           next();
//       }else{
//           next({
//               path:"/login",
//               query:{
//                   redirect:to.fullPath
//               }
//           });
//       }

//   }else{
//       next()
//   }
// });

export default router;
